package com.lin.demo.config;

import net.javacrumbs.shedlock.core.LockProvider;
import net.javacrumbs.shedlock.provider.redis.spring.RedisLockProvider;
import net.javacrumbs.shedlock.spring.annotation.EnableSchedulerLock;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.Objects;

/**
 * @Author zimingl
 * @Date 2023/4/2 16:06
 * @Description: 分布式定时任务锁
 */
@Configuration
@EnableSchedulerLock(defaultLockAtMostFor = "PT30S") //定时任务锁,默认时间30S
public class ShedLockConfig {
//    @Bean
//    public LockProvider lockProvider(DataSource dataSource) {
//        return new JdbcTemplateLockProvider(dataSource);
//    }

//    @Bean
//    public ScheduledLockConfiguration scheduledLockConfiguration(LockProvider lockProvider) {
//        return ScheduledLockConfigurationBuilder.withLockProvider(lockProvider)
//                .withPoolSize(10)
//                .withDefaultLockAtMostFor(Duration.ofMinutes(10))
//                .build();
//    }

    @Bean
    public LockProvider lockProvider(StringRedisTemplate redisTemplate) {
        return new RedisLockProvider(Objects.requireNonNull(redisTemplate.getConnectionFactory()));
    }
}
